package client

import (
	"context"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"
	"kop/framework/wrapper"
	"kop/pb"
	"kop/pb/service"

	questC "kop/modules/quest/client"
)

var srv service.RingService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	////s.Init(test.Flags()...)

	srv = service.NewRingService("go.micro.srv.ring", c)
}

func GetRings(roleID int32) []*pb.Ring {

	var rings, err = srv.GetRings(context.TODO(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}

	return rings.Rings
}

func GetByRingID(roleID, ringID int32) []*pb.Ring {

	var rings, err = srv.GetByRingID(context.TODO(), &service.DefaultRequest{RoleID: roleID, Value: ringID})
	if err != nil {
		panic(err)
	}

	return rings.Rings
}

func GetRing(roleID, ID int32) (*pb.Ring, error) {

	return srv.GetRing(context.TODO(), &service.DefaultRequest{RoleID: roleID, Value: ID})
}

func Create(roleID, ringID int32) *pb.Ring {

	var ring, err = srv.Create(context.TODO(), &service.DefaultRequest{RoleID: roleID, Value: ringID})
	if err != nil {
		panic(err)
	}

	// 日常任务
	questC.DailyQuestRing(roleID, 1)

	return ring
}

func Save(roleID int32, ring *pb.Ring) {

	var r, err = srv.Save(context.TODO(), &service.RingRequest{RoleID: roleID, Ring: ring})
	if err != nil {
		panic(err)
	}

	*ring = *r
}

func Make(roleID, ringID int32, IDs []int32) ([]*pb.Ring, error) {

	var rings, err = srv.Make(context.TODO(), &service.RingMakeRequest{RoleID: roleID, RingID: ringID, IDs: IDs})
	if err != nil {
		return []*pb.Ring{}, err
	}

	return rings.Rings, nil
}

func Delete(IDs *pb.Int32Slice) {

	var _, err = srv.Delete(context.TODO(), IDs)
	if err != nil {
		panic(err)
	}
}
